using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CsvCatcher.Setup
{
    public partial class SetupDlg : Form
    {
        private CsvCatcher.DataSchema.CatcherSettings _sett;
        public SetupDlg(CsvCatcher.DataSchema.CatcherSettings sett)
        {
            InitializeComponent();
            _sett = sett;

            InitRulesPage();
            InitFoldersPage();
        }

        private void InitFoldersPage()
        {
            folderBindingSource.DataSource = _sett.Folders;
            /*this.dataGridView2.DataSource = _sett;

            DataGridViewColumn c1 = new DataGridViewTextBoxColumn();
            c1.HeaderText = "Paths";
            c1.DataPropertyName = "Paths";
            c1.Name = "Paths";
            this.dataGridView2.Columns.Add(c1);*/
        }

        private void InitRulesPage()
        {
            //repositoryItemComboBoxOperationType.Items.AddRange(Enum.GetNames(typeof(DataSchema.OperationType)));

            this.dataGridView1.DataSource = _sett.Rules;
            dataGridView1.AllowUserToAddRows = true;

            DataGridViewColumn c0 = new DataGridViewTextBoxColumn();
            c0.HeaderText = "Name";
            c0.DataPropertyName = "Name";
            c0.Name = "Name";


            DataGridViewColumn c1 = new DataGridViewTextBoxColumn();
            c1.HeaderText = "Pattern";
            c1.DataPropertyName = "Pattern";
            c1.Name = "Pattern";

            DataGridViewColumn c2 = new DataGridViewTextBoxColumn();
            c2.HeaderText = "Path";
            c2.DataPropertyName = "Path";
            c2.Name = "Path";

            //DataGridViewColumn c3 = new DataGridViewTextBoxColumn();
            //c3.HeaderText = "Operation";
            //c3.DataPropertyName = "Operation";
            //c3.Name = "Operation";

            //this.dataGridView1.Columns.AddRange(c0, c1, c2);

        }

        #region EVENTS
        private void onBrowseFolders(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                if(gridViewFolder.ActiveEditor!=null)
                    gridViewFolder.ActiveEditor.Text = fbd.SelectedPath;
                if (gridViewRules.ActiveEditor != null)
                    gridViewRules.ActiveEditor.Text = fbd.SelectedPath;

            }

        }
        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            OperationDlg dlg = new OperationDlg();
            dlg.ShowDialog();
        }

        #region Operations events
        private void OnQueryDiaplayText(object sender, DevExpress.XtraEditors.Controls.QueryDisplayTextEventArgs e)
        {
            object ob = gridViewRules.GetFocusedRow();
            if (ob is CsvCatcher.DataSchema.Rule)
            {
                string disptext = "";
                CsvCatcher.DataSchema.Rule rule = ob as CsvCatcher.DataSchema.Rule;

                rule.Operations.Clear();

                if (this.radioButtonCopy.Checked)
                {
                    rule.Operations.Add(CsvCatcher.DataSchema.OperationType.Copy);
                    disptext += CsvCatcher.DataSchema.OperationType.Copy.ToString();
                }
                else if (radioButtonMove.Checked)
                {
                    rule.Operations.Add(CsvCatcher.DataSchema.OperationType.Move);
                    disptext += CsvCatcher.DataSchema.OperationType.Copy.ToString();
                }
                if (checkBoxUnzip.Checked)
                {
                    rule.Operations.Add(CsvCatcher.DataSchema.OperationType.Unpack);
                    disptext += ";" + CsvCatcher.DataSchema.OperationType.Unpack.ToString();
                }

                e.DisplayText = disptext;
            }
        }

        private void OnPopup(object sender, EventArgs e)
        {
            object ob = gridViewRules.GetFocusedRow();
            if (ob is CsvCatcher.DataSchema.Rule)
            {
                foreach (DataSchema.OperationType op in (ob as CsvCatcher.DataSchema.Rule).Operations)
                {
                    switch (op)
                    {
                        case CsvCatcher.DataSchema.OperationType.Copy:
                            this.radioButtonCopy.Checked = true;
                            break;
                        case CsvCatcher.DataSchema.OperationType.Move:
                            this.radioButtonMove.Checked = true;
                            break;
                        case CsvCatcher.DataSchema.OperationType.Unpack:
                            this.checkBoxUnzip.Checked = true;
                            break;
                    }
                }
            }
        }
        private void OnClosePopup(object sender, EventArgs e)
        {
            this.popupContainerControlOperation.Hide();
        }

        #endregion

    }
}